<?php
namespace app\work\controller;
use app\work\logic\ExtendLogic;

/**
 * 推广工作api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/30
 * Time: 15:31
 */
class Extend extends Base {

    public function __construct(){
        parent::__construct();
        $this->e_logic = new ExtendLogic();
    }

    /**
     * 新增推广工作
     * @return \think\response\Json
     */
    public function ajax_add_extend(){
        $params = input();
        $r = $this->check_user_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        //检查用户规则
        $r = $this->e_logic->check_user_rule();
        if($r['code'] != 1){
            return json($r);
        }
        $u_detail = $r['data']['detail'];

        //检查新增推广工作规则
        $r = $this->e_logic->check_add_extend_rule($params);
        if(!$r['code']){
            return json($r);
        }

        $wre_data = [];
        $wre_data['openid'] = $u_detail['openid'];
        $wre_data['name'] = trim($params['name']);
        $wre_data['content'] = trim($params['content']);
        $wre_data['pic'] = json_encode($params['pic'],JSON_UNESCAPED_UNICODE);
        $wreid = model('work_record_extend')->insertGetId($wre_data);
        if($wreid){
            //同步发送工作圈
            $r = $this->e_logic->sync_send_wcm($u_detail['openid'],$params);

            //更新发布推广工作积分
            $si = new \app\common\controller\SaveIntegral();
            $r = $si->save_re_integral($wreid);

            //生成抄送
            $c = new \app\common\controller\Copy();
            if(!empty($params['copy_openid'])){
                //生成抄送（未抄送）
                $r = $c->gen_copy_not($u_detail['openid'],$wreid,3,$params['copy_openid']);
                //修改抄送（已抄送）
                $r = $c->save_copy_open($u_detail['openid'],$wreid,3);
            }

            return json($this->e_logic->get_response(1,'提交成功'));
        }
        return json($this->e_logic->get_response(1,'提交失败'));
    }
}
